import com.beans.Test1;
import com.beans.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import java.security.cert.X509Certificate;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class AppStart {
    public static void main(String[] args) {
        // 1. 得到 spring 上下文对象
        //    ApplicationContext就是 spring 的代表，表示全局的上下文对象
        //    对象参数就是配置文件的名字
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring-config.xml");

        // 2. 根据上下文对象提供的方法获取到 bean
        //    这里的 beanName必须等于spring-config.xml中的bean的id
        // User user = (User)context.getBean("user");
        // User user = context.getBean(User.class);
        User user = context.getBean("user", User.class);
        // 3. 使用
        user.hello("李四");
//
//        // 第二种获取 bean 对象的方法
//        BeanFactory beanFactory =
//                new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
//        User user1 = (User) beanFactory.getBean("user");
//        user.hello("张三");

        // 比较 ApplicationContext 和 BeanFactory加载资源的区别
        // ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
//        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
//        Test1 test1 = (Test1) beanFactory.getBean("test1");
    }
}
